package ModelLayer;
import java.util.HashMap; 
import java.util.Iterator;

public class DVDContainer
{
    // instance variables
    private HashMap<Long,Dvd> dvds;
    private static DVDContainer instance = null;

    private DVDContainer() { dvds = new HashMap<Long,Dvd>(); }

    public static DVDContainer getInstance() { if (instance == null) instance = new DVDContainer();   return instance; }
    
    
    // Copy CRUD
    public Copy availableCopy(long id) { if(dvds.containsKey(id)) return dvds.get(id).availableCopy();  return null; }
    
    public boolean createCopy(long id, String serialNumber, double purchasePrice)  
    { 
        if(dvds.containsKey(id))  
        {
            dvds.get(id).createCopy(serialNumber, purchasePrice); 
            return true;
        }    
        
        return false;   
    }
    
    public Iterator listDvdCopies(long id)   {   return dvds.get(id).getCopies().iterator(); }
    
    public boolean changeState(long idP, long idD, String serialNo)
    {
        if(serialNo==null) return false;
        
        Iterator it = listAllDVDs();
        while(it.hasNext())
            if(  ( (Dvd)it.next() ).changeState(idP, idD, serialNo)) return true;
        
        return false;
    }

    
    // DVD CRUD
    public void createDVD(long id,String title, String artist, String publicationDate, String description)
    { 
        dvds.put((Long)id, new Dvd(id, title, artist, publicationDate, description));   
    }

    public boolean deleteDVD(long id)  
    {  
        if(dvds.containsKey(id) == false ) return false;
        dvds.remove(id); 
        return true;
    }

    public Dvd getDVD(long id) { if (dvds.containsKey(id)) return dvds.get(id); return null; }   
    
    public boolean updateDVD(long id, String title, String artist, String description)
    {
        if (dvds.containsKey(id) == false || title == null || artist == null || description == null) return false;
        
        Dvd dvd = dvds.get(id);
        dvd.setTitle(title);
        dvd.setArtist(artist);
        dvd.setDescription(description);
        return true;
    }

    public Iterator listAllDVDs() 
    {
        if(dvds.isEmpty()) return null;
        Iterator it = (Iterator) dvds.values().iterator();
        return it;

    }
    
}